﻿using QLBH.Models;
using QLBH.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBH.Modules
{
    public partial class frmAdd_Edit_Category : Form
    {
        private eType eType = eType.ADD;
        private CategoryModel model;
        public CategoryModel Model
        {
            get { return model; }
            set { model = value; }
        }
        public frmAdd_Edit_Category(QLBH.eType eType)
        {
            InitializeComponent();
            this.eType = eType;
            model = new CategoryModel();

        }

        private bool CheckValidData()
        {
            try
            {
                DataInputHelper.CheckTextData(txtName.Text, "Tên đơn vị tính", 30);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Loại Hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.SelectAll();
                txtName.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(txtDescription.Text, "Ghi Chú", 200);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Loại Hàng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtDescription.SelectAll();
                txtDescription.Focus();
                return false;
            }
            return true;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (CheckValidData())
            {
                Model.Industry_id = Int32.Parse(cbbIndustry.SelectedValue.ToString());
                Model.Name = txtName.Text;
                Model.Description = txtDescription.Text;
                //check dieu kien truoc khi chay
                if (eType == eType.ADD)
                {
                    try
                    {
                        CategoryInsertResponse result = WSAccess.Insert<CategoryModel, CategoryInsertResponse>(model, Model.Industry_id + "/category/insert"); ;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    try
                    {
                        WSAccess.Update<CategoryModel>(model, Model.Id + "/category/edit");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                this.DialogResult = DialogResult.None;
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {

        }

        private void LoaDataIndustry()
        {
            List<IndustryModel> data = WSAccess.GetData<List<IndustryModel>>(MainForm.Instance.Shop_id.ToString() + "/industries");
            cbbIndustry.DataSource = data;
            cbbIndustry.DisplayMember = "name";
            cbbIndustry.ValueMember = "id";
        }
        private void frmAdd_Edit_Category_Load(object sender, EventArgs e)
        {
            LoaDataIndustry();
            if (this.eType == eType.ADD)
            {
                this.Text = "Thêm Loại Hàng";
            }
            else
            {
                //load data griview to textbox
                this.Text = "Sửa Loại Hàng";
                txtName.Text = Model.Name;
                txtDescription.Text = Model.Description;
                cbbIndustry.SelectedValue = Model.Industry_id;
            }
        }
    }
    public class CategoryInsertResponse : ResponseBase
    {
        private int category_id;

        public int Category_id
        {
            get { return category_id; }
            set { category_id = value; }
        }

    }
}
